<?php
namespace Admin\Controller;
use Common\Controller\BackendController;
class TejiaController extends BackendController {

    public function _initialize()
    {
        parent::_initialize();
        $this->_mod = D('TejiaCate');
        header('Content-Type:text/html; charset=utf-8');
    }
    public function index()
    {
        $this->display();
    }
    public function setting()
    {
        if (IS_POST) {
            $zym_30 = $this->_post('cate_id', 'trim');					// 本地ID
            $zym_31 = $this->_post('sex', 'trim');
            $zym_28 = $this->_post('tejia_cate_id', 'trim');			// 淘宝ID
            $zym_27 = strtotime($_POST['coupon_start_time']);
            $zym_23 = strtotime($_POST['coupon_end_time']);
            if (!$zym_28) {
                $this->ajaxReturn(0, '采集分类必须选择!');
            }
            if (!$zym_30) {
                $this->ajaxReturn(0, '入库分类必须选择!');
            }
            $zym_25 = $this->_mod->field('cid,name,pid,tid')->where("cid = $zym_28 or tid = $zym_28")->find();
            F('tejia_setting', array('cate_id' => $zym_30, 'sex' => $zym_31, 'cid' => $zym_25['cid'], 'pid' => $zym_25['pid'],'tid'=>$zym_25['tid'], 'coupon_start_time' => $zym_27, 'coupon_end_time' => $zym_23));
            $this->collect();
        }
    }
    public function collect()
    {
        if (false === ($zym_26 = F('tejia_setting'))) {
            $this->ajaxReturn(0, L('illegal_parameters'));
        }
        $zym_33 = $this->_get('p', 'intval', 1);
        $zym_40 = ($zym_33 - 1) * 100;
        $zym_39 = 'http://zhi.taobao.com/json/fantomasItems.htm';
        if (!$zym_26['pid']) {
        	// 如果pid为 0   代表采集大栏目  则获取 tid的值
            $zym_41 = array('appId' => '9', 'blockId' => $zym_26['tid'], 'pageSize' => '100', 'startRow' => $zym_40, 'extQuery' => '', '_input_charset' => 'utf-8', 'sort' => 'null', 'thirdQuery' => 'null');
        } else {
        	// 如果pid不为0 则代表采集下级栏目 则查询下级栏目ID
            $_tom = $this->_mod->where(array('cid' => $zym_26['pid']))->find();
            $zym_41 = array('appId' => '9', 'blockId' => $_tom['tid'], 'pageSize' => '100', 'startRow' => $zym_40, 'extQuery' => 'tagId%3A' . $zym_26['cid'], '_input_charset' => 'utf-8', 'sort' => 'null', 'thirdQuery' => 'null');
        }
        $zym_41 = http_build_query($zym_41);
        \Think\log::record("url $zym_41");
        $zym_38 = curl_init();
        $zym_37 = 300;
        curl_setopt($zym_38, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($zym_38, CURLOPT_URL, $zym_39);
        curl_setopt($zym_38, CURLOPT_REFERER, 'http://www.taobao.com/');
        curl_setopt($zym_38, CURLOPT_POST, true);
        curl_setopt($zym_38, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($zym_38, CURLOPT_POSTFIELDS, $zym_41);
        curl_setopt($zym_38, CURLOPT_ENCODING, 'gb2312');
        curl_setopt($zym_38, CURLOPT_CONNECTTIMEOUT, $zym_37);
        $zym_34 = curl_exec($zym_38);
        
        curl_close($zym_38);
        $zym_34 = rtrim(ltrim(trim($zym_34), '('), ')');
        $zym_35 = json_decode($zym_34, true);
        
        $zym_36 = $zym_35['data'][0]['title'];
        $zym_22 = $zym_35['picServer'];
        if ($zym_33 == 1) {
            $zym_21 = 0;
        } else {
            $zym_21 = F('totalcoll');
        }
        if ($zym_36) {
            for ($zym_7 = 0; $zym_7 < 100; $zym_7++) {
                $zym_8 = $zym_35['data'][$zym_7]['title'];
                $zym_9 = $zym_35['data'][$zym_7]['itemId'];
                $zym_6 = $zym_35['data'][$zym_7]['reservePrice'];
                $zym_5 = $zym_35['data'][$zym_7]['discountPrice'];
                $zym_1 = $zym_35['data'][$zym_7]['discount'];
                $zym_2 = $zym_35['data'][$zym_7]['currentSellOut'];
                $zym_2 = $zym_35['data'][$zym_7]['currentSellOut'];
                $zym_3 = $zym_35['data'][$zym_7]['currentQuantity'];
                $zym_4 = $zym_35['data'][$zym_7]['activityPicUrl'];
                $zym_4 = 'http:' . $zym_22 . '/' . $zym_4;
                $zym_10 = D('Items')->get_tags_by_title($zym_8);
                $zym_11 = implode(',', $zym_10);
                $zym_18['shop_type'] = 'C';
                $zym_18['title'] = $zym_8;
                $zym_18['tags'] = $zym_11;
                $zym_18['intro'] = '原价：' . $zym_6 . '元，折扣后价格：' . $zym_5 . '元，目前已有' . $zym_2 . '人参与抢购。';
                $zym_18['pic_url'] = $zym_4;
                $zym_18['num_iid'] = $zym_9;
                $zym_18['price'] = $zym_6;
                $zym_18['coupon_price'] = $zym_5;
                $zym_18['volume'] = $zym_2;
                $zym_18['ems'] = 1;
                $zym_18['cate_id'] = $zym_26['cate_id'];
                $zym_18['sex'] = $zym_26['sex'];
                $zym_18['coupon_rate'] = $zym_1 * 10;
                $zym_18['coupon_end_time'] = $zym_26['coupon_end_time'];
                $zym_18['coupon_start_time'] = $zym_26['coupon_start_time'];
                $zym_18['astime'] = date('Ymd');
                
                if ($zym_8 && $zym_4 && $zym_9) {
                    $zym_20['item_list'][] = $zym_18;
                }
            }
        } else {
            $this->ajaxReturn(0, '已经采集完成' . $zym_33 . '页！请返回 !');
        }
        foreach ($zym_20['item_list'] as $zym_17 => $zym_16) {
            $zym_12 = $this->_ajax_tb_publish_insert($zym_16);
            if ($zym_12 > 0) {
                $zym_13++;
            }
            $zym_21++;
        }
        F('totalcoll', $zym_21);
        $this->assign('p', $zym_33);
        $this->assign('coll', $zym_13);
        $this->assign('totalnum', $zym_14);
        $this->assign('totalcoll', $zym_21);
        $zym_15 = $this->fetch('collect');
        $this->ajaxReturn(1, '', $zym_15);
    }
    private function _ajax_tb_publish_insert($zym_42)
    {
        $zym_42['title'] = trim(strip_tags($zym_42['title']));
        $zym_20 = D('Items')->ajax_yg_publish($zym_42);
        return $zym_20;
    }
}